﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace AdmLaboratorios.DAO
{
    public static class DAOFactory
    {
        public static string NombreDeUsuario { get; set; }

        //Utilizo una instancia singleton de cada dao.
        static ArrayList daoList = new ArrayList() 
        { 
            new UsuarioDAO(),
            new LogDAO(),
            new PermisoDAO(),
            new PerfilDAO(),
            new UsuarioPerfilDAO(),
            new GrillaColumnaDAO(),
            //--//--//--//--//--//--//--
            new InformeLaboratorioDAO(),
            new InformeRecepcionDAO(),
            new InformeRecepcionItemDAO(),
            new OrdenProduccionDAO(),
            new OrdenProduccionItemDAO(),
            new PartnerDAO(),
            new ProductoDAO(),
            new StockDAO(),
            new ValeConsumoDevolucionDAO(),
            new ValeConsumoDevolucionItemDAO(),
            new PartnerProductoDAO()
        };

        public static object Get(string typeName)
        {
            return Get(Type.GetType("AdmTurnos.DAO." + typeName));
        }

        public static T Get<T>()
        {
            foreach (var item in daoList)
                if (item is T)
                    return (T)item;
            return default(T);
        }

        public static object Get(Type t)
        {
            foreach (var item in daoList)
                if (item.GetType() == t)
                    return item;
            return null;
        }

        public static void PingearConexion()
        {
            try
            {
                DAOHelper.EjecutarScalar("SELECT @@VERSION VersionInfo");
            }
            catch (Exception)
            {
                try
                {
                    //Libero la conexion, si existe.
                    DAOHelper.LiberarConexion();
                    DAOHelper.EjecutarScalar("SELECT @@VERSION VersionInfo");
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

        public static bool ProbarConexion(string cadena, ref string error)
        {
            try
            {
                error = "";

                //Libero la conexion, si existe.
                DAOHelper.LiberarConexion();

                //Seteo el ConnString al Helper.
                DAOHelper.ConnStr = @cadena;

                SqlCommand Cmd = new SqlCommand();
                SqlDataReader dReader;

                bool ret;

                ret = DAOHelper.PrepararConexion(Cmd);
                Cmd.CommandText = "SELECT @@VERSION VersionInfo";
                dReader = DAOHelper.EjecutarReader(Cmd);
                dReader.Close();
                dReader = null;

                return true;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
        }
    }
}
